import { defineStore } from 'pinia';
import { getCurrentInstance } from 'vue';

interface IDrawer {
  modify: boolean;
  /** 导出的地址 */
  filePath: string;
  subTitle: string;
}

export const useDrawerStore = defineStore('drawerStore', {
  state(): IDrawer {
    return {
      modify: false,
      filePath: '',
      subTitle: ''
    };
  },
  actions: {
    changeModify(modify: boolean) {
      this.modify = modify;
    },
    setFilePath(filePath: string) {
      this.filePath = filePath;
    },
    setSubTitle(title: string) {
      this.subTitle = title;
    }
  }
});

export const useExportFile = () => {
  const { setFilePath } = useDrawerStore();
  onUnmounted(() => {
    setFilePath('');
  });
  return {
    setFilePath
  };
};

export const useDispatchBubbles = () => {
  const { proxy } = getCurrentInstance() ?? ({} as any);
  const dispatchBubbleChange = () => {
    proxy?.$el?.dispatchEvent?.(new Event('change', { bubbles: true }));
  };
  return {
    dispatchBubbleChange
  };
};
